<?php

$themename = "NewsOne";
$adminmenuname = "NewsOne Options";
define('SHORTNAME','askopt');

$include_dir = 'includes';
$themeoptions_dir = $include_dir.'/theme-options';

// Functions
require_once($include_dir.'/fn-general.php');
require_once($themeoptions_dir.'/setup.php');

if ( function_exists('register_sidebar') )
{
    register_sidebar(array(
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
        'name' => 'Sidebar',
    ));
}

/**
 * HTML comment list class.
 *
 * @package WordPress
 * @uses Walker
 * @since unknown
 */
class Walker_Comment2 extends Walker {
	/**
	 * @see Walker::$tree_type
	 * @since unknown
	 * @var string
	 */
	var $tree_type = 'comment';

	/**
	 * @see Walker::$db_fields
	 * @since unknown
	 * @var array
	 */
	var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');

	/**
	 * @see Walker::start_lvl()
	 * @since unknown
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param int $depth Depth of comment.
	 * @param array $args Uses 'style' argument for type of HTML list.
	 */
	function start_lvl(&$output, $depth, $args) {
		$GLOBALS['comment_depth'] = $depth + 1;

		echo "<div class='children '>\n";
	}

	/**
	 * @see Walker::end_lvl()
	 * @since unknown
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param int $depth Depth of comment.
	 * @param array $args Will only append content if style argument value is 'ol' or 'ul'.
	 */
	function end_lvl(&$output, $depth, $args) {
		$GLOBALS['comment_depth'] = $depth + 1;

		echo "</div>\n";
	}

	/**
	 * @see Walker::start_el()
	 * @since unknown
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param object $comment Comment data object.
	 * @param int $depth Depth of comment in reference to parents.
	 * @param array $args
	 */
	function start_el(&$output, $comment, $depth, $args) {
		$depth++;
		$GLOBALS['comment_depth'] = $depth;

		if ( !empty($args['callback']) ) {
			call_user_func($args['callback'], $comment, $args, $depth);
			return;
		}

		$GLOBALS['comment'] = $comment;
		extract($args, EXTR_SKIP);
		
		$tag = 'div';
		$add_below = 'div-comment';
?>
		<<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
			<div class="commentmet_data" id="div-comment-<?php comment_ID() ?>">
			<div class="commentmetadata"><span><?php echo get_comment_author_link(); ?></span>在<?php comment_time('Y年m月d日'); ?>制造的噪音</div>
			<div class="commentmet_text"><div class="commentmet_avatar"><?php echo get_avatar( $comment, 64); ?></div><?php comment_text() ?><div class="commentmet_replay"><?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?></div></div>
         </div>
<?php
	}

	/**
	 * @see Walker::end_el()
	 * @since unknown
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param object $comment
	 * @param int $depth Depth of comment.
	 * @param array $args
	 */
	function end_el(&$output, $comment, $depth, $args) {
		if ( !empty($args['end-callback']) ) {
			call_user_func($args['end-callback'], $comment, $args, $depth);
			return;
		}
		echo "</div>\n";
	}

}

/**
 * List comments
 *
 * Used in the comments.php template to list comments for a particular post
 *
 * @since 2.7.0
 * @uses Walker_Comment
 *
 * @param string|array $args Formatting options
 * @param array $comments Optional array of comment objects.  Defaults to $wp_query->comments
 */
function wp_list_comments2($args = array(), $comments = null ) {
	global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;

	$in_comment_loop = true;

	$comment_alt = $comment_thread_alt = 0;
	$comment_depth = 1;

	$defaults = array('walker' => null, 'max_depth' => '', 'style' => 'div', 'callback' => null, 'end-callback' => null, 'type' => 'all',
		'page' => '', 'per_page' => '', 'avatar_size' => 86, 'reverse_top_level' => null, 'reverse_children' => '');

	$r = wp_parse_args( $args, $defaults );

	// Figure out what comments we'll be looping through ($_comments)
	if ( null !== $comments ) {
		$comments = (array) $comments;
		if ( empty($comments) )
			return;
		if ( 'all' != $r['type'] ) {
			$comments_by_type = &separate_comments($comments);
			if ( empty($comments_by_type[$r['type']]) )
				return;
			$_comments = $comments_by_type[$r['type']];
		} else {
			$_comments = $comments;
		}
	} else {
		if ( empty($wp_query->comments) )
			return;
		if ( 'all' != $r['type'] ) {
			if ( empty($wp_query->comments_by_type) )
				$wp_query->comments_by_type = &separate_comments($wp_query->comments);
			if ( empty($wp_query->comments_by_type[$r['type']]) )
				return;
			$_comments = $wp_query->comments_by_type[$r['type']];
		} else {
			$_comments = $wp_query->comments;
		}
	}

	if ( '' === $r['per_page'] && get_option('page_comments') )
		$r['per_page'] = get_query_var('comments_per_page');

	if ( empty($r['per_page']) ) {
		$r['per_page'] = 0;
		$r['page'] = 0;
	}

	if ( '' === $r['max_depth'] ) {
		if ( get_option('thread_comments') )
			$r['max_depth'] = get_option('thread_comments_depth');
		else
			$r['max_depth'] = -1;
	}

	if ( '' === $r['page'] ) {
		if ( empty($overridden_cpage) ) {
			$r['page'] = get_query_var('cpage');
		} else {
			$threaded = ( -1 == $r['max_depth'] ) ? false : true;
			$r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
			set_query_var( 'cpage', $r['page'] );
		}
	}
	// Validation check
	$r['page'] = intval($r['page']);
	if ( 0 == $r['page'] && 0 != $r['per_page'] )
		$r['page'] = 1;

	if ( null === $r['reverse_top_level'] )
		$r['reverse_top_level'] = ( 'desc' == get_option('comment_order') ) ? TRUE : FALSE;

	extract( $r, EXTR_SKIP );

	if ( empty($walker) )
		$walker = new Walker_Comment2;

	$walker->paged_walk($_comments, $max_depth, $page, $per_page, $r);
	$wp_query->max_num_comment_pages = $walker->max_pages;

	$in_comment_loop = false;
}

function share_buttons($content) {
	if(is_single())
		$content = '<p><strong>如果您觉得这里的内容还不错，请通过 <a href="http://feed.xirang.us" target="_blank">Feedsky</a> 或是 <a href="http://feeds.feedburner.com/xlrw" target="_blank">Feedburner</a> 订阅本站文章全文RSS更新。</strong></p>'. $content .'<p><strong>喜欢这篇文章就收藏她吧&nbsp;</strong><a href="javascript:var%20b=document.body;var%20GR________bookmarklet_domain=\'http://www.google.com\';if(b&&!document.xmlVersion){void(z=document.createElement(\'script\'));void(z.src=\'http://www.google.com/reader/ui/link-bookmarklet.js\');void(b.appendChild(z));}else{}" title="Share in Google Reader" rel="nofollow"><img src="http://www.google.com/reader/ui/favicon.ico" alt="分享到 Google Reader" /></a><a href="http://www.addtoany.com/share_save" target="_blank"><img src="http://static.addtoany.com/buttons/share_save_106_16.gif" alt="Share/Save/Bookmark"/></a>&nbsp;<a class="dt_button" href="http://www.daitui.com/bookmark" target="_blank"><img src="http://src.daitui.com/widget/02.gif" alt="收藏与分享" /></a>&nbsp;<script charset="utf8" type="text/javascript">cT="0";nc="#444444";nBgc="";nBorder="#B11418";tc="#FFFFFF";tBgc="#FE1923";tBorder="#B11418";tDigg="%E6%8E%A8%E8%8D%90%E5%88%B0%E9%B2%9C%E6%9E%9C";tDugg="%E6%82%A8%E5%B7%B2%E7%BB%8F%E6%8E%A8%E8%8D%90";defaultItemUrl="WEB_URL";defaultFeedUrl ="http://feed.xirang.us";</script><script type="text/javascript" charset="utf8" src="http://re.xianguo.com/api/diggthis.js"></script></p>';
	else if(is_feed())
		$content = '<p><span style="color: #ff6600;">站长提示：如果您点击本文中的链接后出现了广告画面，麻烦您等待5秒后点击右上角的</span><strong>SKIP AD</strong><span style="color: #ff6600;">按钮跳过广告继续查看链接内容。对您造成的不便我们深感歉意，但是这一点微薄的广告收入可以维持本站的正常运作，全站作者由衷地感谢您的谅解与支持。</span></p>' . $content;
	if(false != strpos($content,'[flashvideo'))
			$content = '<script type="text/javascript" src="/js/swfobject.js"></script>' . "\n" . $content;
	return $content;
}


function the_copyright($content)
{
	if(is_single() || is_feed())
		$content .= generate_copyright(get_post_meta(get_the_ID(), "copyright", true));
	return $content;
}

function generate_copyright($copyright = "none", $userinfo = true)
{
	switch($copyright)
	{
		case 'sa':
		case 'nd':
		case '':
			$info .='<h3>著作权信息（站外使用本文请保留以下内容）</h3><hr size="2" />';
			break;
		case 'tr':
			$info .='<h3>译文著作权信息（站外使用本文请保留以下内容）</h3><hr size="2" />';
			break;
		case 'none':
		default:
			if($userinfo)
			$info .='<h3>文章信息</h3><hr size="2" />';
			break;
	}
	if($userinfo) {
		
		$info .= '<table width="100%"><tbody><tr><td>';
		
		if($copyright == 'tr')
			$info .= '译文作者：';
		else if($copyright == 'none')
			$info .= '转载作者：';
		else
			$info .= '文章作者：';
		
		$info .='<a  href="/author/' . get_the_author_meta('login', false , false) . '">'. get_the_author() . '</a><br>' . __('原始站点：') . '<a title="嘻来嚷往" href="/" target="_self">嘻来嚷往 - IF YOU SEE SOMETHING, SAY SOMETHING.</a>' . '<br>' . __('原文标题：') . get_the_title('', '', false) . '<br>' . __('发表日期：') . get_the_time(__('Y年m月d日'), false) . '<br>' .  __('原文链接：') . '<a href="'. get_permalink() .'" >' . get_permalink() . '</a><br></td><td class="author_img"><center>' . get_avatar(get_the_author_id(), '100') . '</center></td></tr>';
	}
	switch($copyright)
	{
		case 'sa':
			$info .= get_by_nc_sa();
			break;
		case 'nd':
		case '':
			$info .= get_by_nc_nd();
			break;
		case 'none':
		default:
			break;
	}
	if($copyright != 'none' || $userinfo != false)
		$info .='</tbody></table><hr size="2" />';
	return $info;
}

function get_by_nc_nd()
{
	return '<tr><td>版权协议：本作品采用<a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.5/cn/" target="_blank">知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议</a>进行许可，作品中的文字和图像若非明确指出著作权归属则均受到该协议<a title="知识共享协议法律文本" href="http://creativecommons.org/licenses/by-nc-nd/2.5/cn/legalcode" target="_blank">法律文本</a>的保护。站外使用请严格遵守协议，<a href="/" target="_self">嘻来嚷往</a>站保留诉诸法律的权利！如有异议请与站长<a title="联系我们" href="/affairs" target="_self">联系</a>。</td><td><center><img src="http://i.creativecommons.org/l/by-nc-nd/2.5/cn/88x31.png" alt="BY-NC-ND" /></center></td></tr>';
}

function get_by_nc_sa()
{
	return '<tr><td>版权协议：本作品采用<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank">知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议</a>进行许可，作品中的文字和图像若非明确指出著作权归属则均受到该协议<a title="知识共享协议法律文本" href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/legalcode" target="_blank">法律文本</a>的保护。站外使用请严格遵守协议，<a href="/" target="_self">嘻来嚷往</a>站保留诉诸法律的权利！如有异议请与站长<a title="联系我们" href="/affairs" target="_self">联系</a>。</td><td><center><img src="http://i.creativecommons.org/l/by-nc-sa/2.5/cn/88x31.png" alt="BY-NC-SA" /></center></td></tr>';
}


$linkwithin_code_start = "//LinkWithinCodeStart";
$linkwithin_code_end   = "//LinkWithinCodeEnd";

function linkwithin_add_hook($content) {
    if (!is_feed() && !is_page()) {
        global $post, $wp_query, $linkwithin_code_start, $linkwithin_code_end;

 //       $permalink = get_permalink($post->ID);
//		if('</p>' != substr($content, strlen($content)-4, strlen($content)-1)) $content .= '</p>';
        $content .= '<div class="linkwithin_hook"></div>';
        $content = linkwithin_add_code($content);
    }
    return $content;
}

function linkwithin_add_code($content) {
    if (!is_feed() && !is_page()) {
        global $post, $wp_query, $linkwithin_code_start, $linkwithin_code_end;

        if ($wp_query->current_post + 1 == $wp_query->post_count) {

            $embed_code = "<script type=\"text/javascript\">
var linkwithin_site_id = 47742;
</script>
<script type=\"text/javascript\" src=\"http://www.linkwithin.com/widget.js\"></script>
<a href=\"http://www.linkwithin.com/\"><img src=\"http://www.linkwithin.com/pixel.png\" alt=\"Related Posts with Thumbnails\" style=\"border: 0\" /></a>";
            $embed_code = str_replace("<script type=\"text/javascript\">", "\n<script type=\"text/javascript\"><!-- $linkwithin_code_start\nvar linkwithin_div_class=\"linkwithin_hook\";", $embed_code);
            $embed_code = str_replace(";\n</script>", ";\n$linkwithin_code_end -->\n</script>", $embed_code);
            $content .= $embed_code;
        }
    }
    return $content;
}

function linkwithin_display_excerpt($content){
    global $linkwithin_code_start, $linkwithin_code_end;
    $posStart = strpos($content, $linkwithin_code_start);
    $posEnd = strpos($content, $linkwithin_code_end);
    if ($posStart){
        if ($posEnd == false){
            $content = str_replace(substr($content,$posStart,strlen($content)),'',$content);
        } else {
            $content = str_replace(substr($content,$posStart,$posEnd-$posStart+strlen($linkwithin_code_end)),'',$content);
        }
    }
    $content = $content . linkwithin_add_code('');
    return $content;
}

function the_content_hook($content){
//	if('</p>' != substr($content, strlen($content)-4, strlen($content)-1))
//		$content .= '</p>';
	if(false != strpos($content,'[flashvideo'))
		$content = '<script type="text/javascript" src="/js/swfobject.js"></script>' . "\n" . $content;
	
	if(is_single() || is_feed()) {
		$content .= generate_copyright(get_post_meta(get_the_ID(), "copyright", true));
		if(is_single()) {
			$content = '<p><strong>如果您觉得这里的内容还不错，请通过 <a href="http://feed.xirang.us" target="_blank">Feedsky</a> 或是 <a href="http://feeds.feedburner.com/xlrw" target="_blank">Feedburner</a> 订阅本站全文RSS更新。</strong></p>'. $content .'<p><strong>收藏本文&nbsp;</strong><a href="http://www.addtoany.com/share_save" target="_blank"><img src="http://static.addtoany.com/buttons/share_save_106_16.gif" alt="Share/Save/Bookmark"/></a>&nbsp;<a class="dt_button" href="http://www.daitui.com/bookmark" target="_blank"><img src="http://src.daitui.com/widget/02.gif" alt="收藏与分享" /></a>&nbsp;<script charset="utf8" type="text/javascript">cT="0";nc="#444444";nBgc="";nBorder="#B11418";tc="#FFFFFF";tBgc="#FE1923";tBorder="#B11418";tDigg="%E6%8E%A8%E8%8D%90%E5%88%B0%E9%B2%9C%E6%9E%9C";tDugg="%E6%82%A8%E5%B7%B2%E7%BB%8F%E6%8E%A8%E8%8D%90";defaultItemUrl="WEB_URL";defaultFeedUrl ="http://feed.xirang.us";</script><script type="text/javascript" charset="utf8" src="http://re.xianguo.com/api/diggthis.js"></script></p>';
		}
		else if(is_feed()) {
			$begin = '<p style="text-align:center;font-size:large;">&copy; <a href="http://xirang.us/author/' . get_the_author_meta('login', false , false) . '" target="_blank">'. get_the_author() .'</a> & <a href="http://xirang.us" target="_blank">嘻来嚷往</a> | <a href="'. get_permalink() .'" target="_blank">原文链接</a> | <a href="'. get_permalink().'#comments" target="_blank">参与评论</a> | <a href="http://jizha.xirang.us" target="_blank" title="多媒体微型博客社区">叽喳</a> | <a href="https://twitter.com/xirang" target="_blank" title="嘻来嚷往官方Twitter">Twitter</a></p>';

			if (false != strpos($content,'http://adf.ly/'))
				$begin .= '<p><span style="color: #ff6600;">站长提示：如果您点击本文中的链接后出现了广告画面，麻烦您等待5秒后点击右上角的</span><strong>SKIP AD</strong><span style="color: #ff6600;">按钮跳过广告继续查看链接内容。对您造成的不便我们深感歉意，但是这一点微薄的广告收入可以维持本站的正常运作，全站作者由衷地感谢您的谅解与支持。</span></p>';

			$content = $begin . '<hr size="2" color="#bd1016"/>' . $content . '<hr size="2" color="#bd1016"/><p style="text-align: center;"><a href="http://xirang.us/r.php?t=a&amp;id=justhost" target="_blank" title=""><img src="http://affiliates.justhost.com/control/img/banners/justhost_728x90v4.gif" alt="" width="610" height="75" /></a></p><p style="text-align: center;">现在从上图点击进入<a href="http://xirang.us/r.php?t=a&amp;id=justhost" target="_blank">Just Host</a>网站，并在结帐时使用<span style="background-color: #ffff00; color: #ff0000;">50OFF</span>优惠码，将获得最低2.95 USD/月的优惠价格</p><p style="TEXT-ALIGN: center"><a href="http://xirang.us/r.php?t=a&amp;id=ix" target="_blank"><img  src="http://www.ixwebhosting.com/templates/ix/v2/images/banners/expert-468x60-feb-22.gif" alt="" width="610" height="78" /></a></p><p style="text-align: center;">现在从上图点击进入<a href="http://xirang.us/r.php?t=a&amp;id=ix" target="_blank">IX Webhosting</a>网站，并在结帐时使用<span style="background-color: #ffff00; color: #ff0000;">secretlink20</span>优惠码，</span><span style="font-size: small;">将再获得20%优惠</p>';
		}
	}
	return $content;
}

function cut_utf8($str='',$len=0) {
	$olen = strlen($str);
    if (!isset($str[$len])) {
    } else {
        if (seems_utf8($str[$len-1]))
            $str = substr($str, 0, $len);
        else {
            if(seems_utf8($str[$len-3].$str[$len-2].$str[$len-1]))
                $str = substr($str, 0, $len-3) . $str[$len-3] . $str[$len-2] . $str[$len-1];

            elseif(seems_utf8($str[$len-2].$str[$len-1].$str[$len]))
                $str = substr($str, 0, $len-2) . $str[$len-2].$str[$len-1].$str[$len];

            elseif(seems_utf8($str[$len-1].$str[$len].$str[$len+1]))
                $str = substr($str, 0, $len-1) . $str[$len-1].$str[$len].$str[$len+1];

            else
                $str = substr($str, 0, $len);
        }
    }
	if($olen > strlen($str)) $str .= '……';
    return $str;
}

add_filter('the_content', 'the_content_hook', 0);
add_filter('the_content', 'linkwithin_add_hook', 1);
add_filter('the_excerpt', 'linkwithin_display_excerpt');
?>